package com.example.leo.mycharmingstudent.a08280829;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.leo.mycharmingstudent.R;

/**
 * Created by Leo on 2020/8/28.
 */

public class ClazzPracticeFragment extends Fragment {

    private Context mContext;
    private String mName;

    //开发过程中，不要直接使用字符串，定义常量
    //为什么呢？
    //1.避免手残打错字符串  2.一次修改，多地受用
    public static final String FRAGMENT_NAME = "fragment_name";
    public static final String FRAGMENT_SEX = "fragment_sex";
    public static final String FRAGMENT_AGE = "fragment_age";
    public static final String FRAGMENT_WEIGHT = "fragment_weight";
    public static final String FRAGMENT_HEIGHT = "fragment_height";
    private String mSex;
    private String mAge;
    private String mWeight;
    private String mHeight;
    //显示的内容
    private TextView tvContent;

    private static final String PRETTY_GIRL = "美丽的女孩子，请吃面包片";

    /**
     * 当执行到这个方法的时候，
     * getContext()获取到的上下文菜单不为null
     *
     * @param savedInstanceState
     */
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mContext = getContext();
    }

    public static Fragment newInstance() {
        ClazzPracticeFragment clazzPracticeFragment = new ClazzPracticeFragment();
        return clazzPracticeFragment;
    }

    /**
     * 封装静态方法，创建Fragment对象
     * @param name
     * @return
     */
    public static Fragment newInstance(String name) {
        ClazzPracticeFragment clazzPracticeFragment = new ClazzPracticeFragment();
        Bundle bundle = new Bundle();
        bundle.putString(FRAGMENT_NAME, name);
        clazzPracticeFragment.setArguments(bundle);
        return clazzPracticeFragment;
    }

    public static ClazzPracticeFragment newInstance(
            String name,
            String sex,
            String age,
            String weight,
            String height) {
        ClazzPracticeFragment clazzPracticeFragment = new ClazzPracticeFragment();
        Bundle bundle = new Bundle();
        bundle.putString(FRAGMENT_NAME, name);
        bundle.putString(FRAGMENT_SEX,sex);
        bundle.putString(FRAGMENT_AGE,age);
        bundle.putString(FRAGMENT_WEIGHT,weight);
        bundle.putString(FRAGMENT_HEIGHT,height);
        //从Activity向Fragment传值，通过Bundle的方式
        clazzPracticeFragment.setArguments(bundle);
        return clazzPracticeFragment;
    }

    /**
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     * @Nullable表示可以为空
     * @NonNull 表示不可以为空
     */
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Bundle arguments = getArguments();
        if (arguments != null) {
            this.mName = arguments.getString(FRAGMENT_NAME);
            mSex = arguments.getString(FRAGMENT_SEX);
            mAge = arguments.getString(FRAGMENT_AGE);
            mWeight = arguments.getString(FRAGMENT_WEIGHT);
            mHeight = arguments.getString(FRAGMENT_HEIGHT);
        }
        View clazzPracticeFragmentView = inflater.inflate(R.layout.clazz_practice_fragment, null, false);
        tvContent = clazzPracticeFragmentView.findViewById(R.id.tv_content_clazz_practice_fragment);
        final String format = String.format("%s-%s-%s-%s-%s", mName, mSex, mAge, mWeight, mHeight);
        tvContent.setText(format);
        tvContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*//获取的是关联的那个Activity或者说上下文菜单
                Context context = ClazzPracticeFragment.this.getContext();
                //instanceof 判断对象是不是某个类型
                //此处的含义是：判断context是不是FragmentClazzActivity类型
                if (context instanceof FragmentClazzActivity) {
                    FragmentClazzActivity fragmentClazzActivity =
                            (FragmentClazzActivity) context;
                    //匿名内部类访问局部变量，这个局部变量必须final
                    fragmentClazzActivity.setShowInfo(format);
                }*/
                mOnListener.changeText(format);
                mOnListener.showToast(/*魔法值"adsf adsf asdf ads"*/PRETTY_GIRL);
            }
        });
        return clazzPracticeFragmentView;
    }

    public void setContent(String text){
        tvContent.setText(text);
    }

    /**
     * Fragment调用Activity的方法使用
     */
    public interface OnListener {
        void changeText(String text);

        void showToast(String toast);
    }

    private OnListener mOnListener;

    public void setOnListener(OnListener onListener){
        this.mOnListener = onListener;
    }
}
/*
使用接口的方式，Fragment调用Activity的方法基本流程：
1.创建一个接口，理论上定义成普通接口或者内部接口（内部类）都可以
2.定义一个接口引用
3.实现这个接口
4.传入接口实现，也就是mOnListener对象
5.Fragment当中直接使用mOnListener对象
*/
